package cn.kgc.springbootdemo.config.dingding;

import cn.hutool.http.HttpRequest;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;


/**
 * <p>
 * 钉钉机器人群警告
 * </p>
 *
 * @author 董泽建
 * @Version: V1.0
 * @since 2019-03-14 13:01
 */
@UtilityClass
@Slf4j
public class DingdingUtils {
    private static OkHttpClient client = new OkHttpClient();

    /**
     * 发送钉钉消息 * @param jsonString 消息内容 * @param webhook 钉钉自定义机器人webhook * @return
     */
    public boolean sendToDingding(String jsonString, String webhook) {
        try {
            String results = HttpRequest.post(webhook)
                    .body(jsonString)
                    .timeout(20000)
                    .execute().body();
            log.debug("钉钉发送信息响应信息->{}",results);
            return true;
        } catch (Exception e) {
            log.error("钉钉报警发送失败 : {}", e);
            return false;
        }
    }
}
